﻿//-----------------------------------------------------------------------
// <copyright company="Omron Corporation">
// Copyright © Omron Corporation 2013. All Rights Reserved
// </copyright>
//
// COMPANY CONFIDENTIAL.
//-----------------------------------------------------------------------

namespace Attacher.Tests
{
	using System;
	using System.Windows;

	using Attacher;

	using Microsoft.VisualStudio.TestTools.UnitTesting;

	[TestClass]
	public class ParameterCheckerTest
	{
		[TestMethod]
		public void TestBetweenOk1()
		{
			ParameterChecker.IsBetween<int>(3, 2, 4, "test");
			ParameterChecker.IsBetween<int>(3, 2, 4, "test", "test");
		}

		[TestMethod]
		public void TestBetweenOk2()
		{
			ParameterChecker.IsBetween<int>(3, -2, 4, null);
			ParameterChecker.IsBetween<int>(3, -2, 4, null, null);
		}

		[TestMethod]
		public void TestBetweenOk3()
		{
			ParameterChecker.IsBetween<int>(-3, -4, -2, null);
			ParameterChecker.IsBetween<int>(-3, -4, -2, null, null);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestBetweenFail1()
		{
			ParameterChecker.IsBetween<int>(3, 5, 10, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestBetweenFail1A()
		{
			ParameterChecker.IsBetween<int>(3, 5, 10, "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestBetweenFail2()
		{
			ParameterChecker.IsBetween<int>(3, 0, 2, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestBetweenFail2A()
		{
			ParameterChecker.IsBetween<int>(3, 0, 2, "test", "jj");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestBetweenFail3()
		{
			ParameterChecker.IsBetween<int>(-3, 2, 3, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestBetweenFail3A()
		{
			ParameterChecker.IsBetween<int>(-3, 2, 3, "test", null);
		}

		[TestMethod]
		public void TestGreaterOrEqualsOk1()
		{
			ParameterChecker.IsGreaterOrEquals<int>(-3, -7, "test");
			ParameterChecker.IsGreaterOrEquals<int>(-3, -7, "test", null);
		}

		[TestMethod]
		public void TestGreaterOrEqualsOk2()
		{
			ParameterChecker.IsGreaterOrEquals<int>(-3, -3, "test");
			ParameterChecker.IsGreaterOrEquals<int>(-3, -3, "test", "jdjdj");
		}

		[TestMethod]
		public void TestGreaterOrEqualsOk3()
		{
			ParameterChecker.IsGreaterOrEquals<int>(3, 3, "test");
			ParameterChecker.IsGreaterOrEquals<int>(3, 3, "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestGreaterOrEqualsFail1()
		{
			ParameterChecker.IsGreaterOrEquals<int>(-3, -2, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestGreaterOrEqualsFail1A()
		{
			ParameterChecker.IsGreaterOrEquals<int>(-3, -2, "test", "kd");
		}

		[TestMethod]
		public void TestGreaterOk1()
		{
			ParameterChecker.IsGreaterThan<int>(3, 2, "test");
			ParameterChecker.IsGreaterThan<int>(3, 2, "test", "kk");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestGreaterFail1()
		{
			ParameterChecker.IsGreaterThan<int>(3, 3, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestGreaterFail1A()
		{
			ParameterChecker.IsGreaterThan<int>(3, 3, "test", string.Empty);
		}

		[TestMethod]
		public void TestLessOrEqualsOk1()
		{
			ParameterChecker.IsLessOrEquals<int>(3, 4, "test");
			ParameterChecker.IsLessOrEquals<int>(3, 4, "test", null);
		}

		[TestMethod]
		public void TestLessOrEqualsOk2()
		{
			ParameterChecker.IsLessOrEquals<int>(3, 3, "test");
			ParameterChecker.IsLessOrEquals<int>(3, 3, "test", null);
		}

		[TestMethod]
		public void TestLessOrEqualsOk3()
		{
			ParameterChecker.IsLessOrEquals<int>(-3, -2, "test");
			ParameterChecker.IsLessOrEquals<int>(-3, -2, "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestLessOrEqualsFail1()
		{
			ParameterChecker.IsLessOrEquals<int>(4, 3, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestLessOrEqualsFail1A()
		{
			ParameterChecker.IsLessOrEquals<int>(4, 3, "test", string.Empty);
		}

		[TestMethod]
		public void TestIsEqualsOk1()
		{
			ParameterChecker.IsEquals<int>(2, 2, "test");
			ParameterChecker.IsEquals<int>(2, 2, "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestIsEqualsFails1()
		{
			ParameterChecker.IsEquals<int>(-2, 2, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestIsEqualsFails2()
		{
			ParameterChecker.IsEquals<int>(-2, 2, "test", null);
		}

		[TestMethod]
		public void TestLessThanOk1()
		{
			ParameterChecker.IsLessThan<int>(-3, -2, "test");
			ParameterChecker.IsLessThan<int>(-3, -2, "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestLessThanFail1()
		{
			ParameterChecker.IsLessThan<int>(-2, -2, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void TestLessThanFail1A()
		{
			ParameterChecker.IsLessThan<int>(-2, -2, "test", null);
		}

		[TestMethod]
		public void TestNotEmptyOk()
		{
			ParameterChecker.IsNotEmpty<int>(new int[] { 1, 2 }, "test");
			ParameterChecker.IsNotEmpty<int>(new int[] { 1, 2 }, "test", null);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestNotEmptyFail1()
		{
			ParameterChecker.IsNotEmpty<int>(new int[] { }, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestNotEmptyFail1A()
		{
			ParameterChecker.IsNotEmpty<int>(new int[] { }, "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestNotEmptyFail()
		{
			ParameterChecker.IsNotEmpty<int>(null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestNotEmptyFailA()
		{
			ParameterChecker.IsNotEmpty<int>(null, "test", "dd");
		}

		[TestMethod]
		public void TestNotNullOk()
		{
			ParameterChecker.IsNotNull(new int[] { 1, 2 }, "test");
			ParameterChecker.IsNotNull(new int[] { 1, 2 }, "test", "kdkdk");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestNotNullFail()
		{
			ParameterChecker.IsNotNull(null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestNotNullFailA()
		{
			ParameterChecker.IsNotNull(null, "test", null);
		}

		[TestMethod]
		public void TestNotNullOrEmptyOk1()
		{
			ParameterChecker.IsNotNullOrEmpty(".", "test");
			ParameterChecker.IsNotNullOrEmpty(".", "test", string.Empty);
		}

		[TestMethod]
		public void TestNotNullOrEmptyOk2()
		{
			ParameterChecker.IsNotNullOrEmpty("\n.\n", "test");
			ParameterChecker.IsNotNullOrEmpty("\n.\n", "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestNotNullOrEmptyFail1()
		{
			ParameterChecker.IsNotNullOrEmpty(null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void TestNotNullOrEmptyFail1A()
		{
			ParameterChecker.IsNotNullOrEmpty(null, "test", null);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestNotNullOrEmptyFail2()
		{
			ParameterChecker.IsNotNullOrEmpty(string.Empty, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestNotNullOrEmptyFail2A()
		{
			ParameterChecker.IsNotNullOrEmpty(string.Empty, "test", string.Empty);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestNotNullOrEmptyFail3()
		{
			ParameterChecker.IsNotNullOrEmpty("    ", "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void TestNotNullOrEmptyFail3A()
		{
			ParameterChecker.IsNotNullOrEmpty("    ", "test", "kkd");
		}

		[TestMethod]
		public void ParameterChecker_IsOfType_Works()
		{
			ParameterChecker.IsOfType(new Version(), typeof(Version), "test");
		}

		[TestMethod]
		public void ParameterChecker_IsOfTypeWithMessage_Works()
		{
			ParameterChecker.IsOfType(new Version(), typeof(Version), "test", "message");
		}

		[TestMethod]
		public void ParameterChecker_IsOfType_Throws()
		{
			// Arrange - Act
			try
			{
				ParameterChecker.IsOfType(new Version(), typeof(string), "test");
				Assert.Fail("No exception thrown");
			}
			catch (ArgumentException ex)
			{
				// Assert				
				Assert.AreEqual("test", ex.ParamName);
			}
		}

		[TestMethod]
		public void ParameterChecker_IsOfTypeWithMessage_Throws()
		{
			// Arrange - Act
			try
			{
				ParameterChecker.IsOfType(new Version(), typeof(string), "test", "message");
				Assert.Fail("No exception thrown");
			}
			catch (ArgumentException ex)
			{
				// Assert				
				Assert.AreEqual("test", ex.ParamName);
				Assert.IsTrue(ex.Message.StartsWith("message"));
			}
		}

		[TestMethod]
		public void ParameterChecker_IsValidEnum_Works()
		{
			// Arrage - Act - Assert
			ParameterChecker.IsValidEnum(MessageBoxButton.OK, typeof(MessageBoxButton), "test");
		}

		[TestMethod]
		public void ParameterChecker_IsValidEnum_Works2()
		{
			// Arrage - Act - Assert
			ParameterChecker.IsValidEnum(MessageBoxButton.OK, typeof(MessageBoxButton), "test", "message");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void ParameterChecker_IsValidEnum_Throws()
		{
			// Arrage - Act - Assert
			ParameterChecker.IsValidEnum(MessageBoxImage.Error, typeof(MessageBoxButton), "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void ParameterChecker_IsValidEnum_Throws2()
		{
			// Arrage - Act - Assert
			ParameterChecker.IsValidEnum(MessageBoxImage.Error, typeof(MessageBoxButton), "test", "message");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void ParameterChecker_IsValidEnum_Throws3()
		{
			// Arrage - Act - Assert
			ParameterChecker.IsValidEnum(new DataFormat("test", 2), typeof(DataFormat), "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void ParameterChecker_IsValidEnum_Throws4()
		{
			// Arrage - Act - Assert
			ParameterChecker.IsValidEnum(new DataFormat("test", 2), typeof(DataFormat), "test", "message");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void ParameterChecker_IsValidEnum_Throws5()
		{
			// Arrage - Act - Assert
			ParameterChecker.IsValidEnum((MessageBoxImage)100, typeof(MessageBoxImage), "test", "message");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ParameterChecker_IsGreaterThan_LowerValueMandatory()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsGreaterThan(string.Empty, null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ParameterChecker_IsGreaterOrEquals_LowerValueMandatory()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsGreaterOrEquals(string.Empty, null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ParameterChecker_IsEquals_ComparisonValueMandatory()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsEquals(string.Empty, null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ParameterChecker_IsLessThan_UpperValueMandatory()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsLessThan(string.Empty, null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ParameterChecker_IsLessOrEquals_UpperValueMandatory()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsLessOrEquals(string.Empty, null, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void IsMultipleOf_ValueIsNotFactor_ThrowsArgumentExceptionWithDefaultMessage()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsMultipleOf(1000, 999, "test");
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentException))]
		public void IsMultipleOf_ValueIsNotFactor_ThrowsArgumentExceptionWithCustomMessage()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsMultipleOf(1000, 999, "test", "message");
		}

		[TestMethod]
		public void IsMultipleOf_ValueIsFactor_Passes()
		{
			// Arrange - Act - Assert
			ParameterChecker.IsMultipleOf(1000, 10, "test", "message");
		}
	}
}
